import java.util.concurrent.atomic.AtomicInteger;

/**
 * CAS使用
 */
public class CASDemo1 {
    private static int number=0;
    private static AtomicInteger atomicInteger=new AtomicInteger(0);
    private final static int MAX_COUNT=100000;
    public static void main(String[] args) throws InterruptedException {

        //++
        Thread t1=new Thread(()->{
            for (int i = 0; i <MAX_COUNT ; i++) {
               atomicInteger.getAndIncrement();
            }
        });
        t1.start();
        //--
        Thread t2=new Thread(()->{
            for (int i = 0; i <MAX_COUNT ; i++) {
               atomicInteger.getAndDecrement();
            }
        });
        t2.start();
        t1.join();
        t2.join();
        System.out.println("最终结果："+atomicInteger.get());

    }
}

